package com.zlfund.cat.zjgsmk.util;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * SharedPreference操作类
 */
public class SpUtils {
    public static final String GLOBALNAME = "XZG";
    public static final String USERINFO = "USERINFO";
    public static final String ACCOUNTINFO = "ACCOUNTINFO";
    public static final String ISFIRSTIN = "isFirstIn";
    public static final String VERSION = "version";
    public static final String ISFIRSTEXIT = "isFirstExit";

    public static String getStringFromGlobal(Context context, String strKey) {
        SharedPreferences setPreferences = context.getSharedPreferences(
                GLOBALNAME, Context.MODE_PRIVATE);
        return setPreferences.getString(strKey, "");
    }

    public static String getStringFromGlobal(Context context, String strKey,
                                             String strDefault) {
        SharedPreferences setPreferences = context.getSharedPreferences(
                GLOBALNAME, Context.MODE_PRIVATE);
        return setPreferences.getString(strKey, strDefault);
    }

    public static void putStringToGlobal(Context context, String strKey, String strData) {
        SharedPreferences activityPreferences = context.getSharedPreferences(
                GLOBALNAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = activityPreferences.edit();
        editor.putString(strKey, strData);
        editor.apply();
    }

//    public static void putRiskToGlobal(Context context, RiskBean bean){
//        SharedPreferences activityPreferences = context.getSharedPreferences(
//                GLOBALNAME, Context.MODE_PRIVATE);
//        SharedPreferences.Editor editor = activityPreferences.edit();
//        editor.putInt(SpConstants.RISK_LEVEL, bean.getRiskLevel());
//        editor.putString(SpConstants.RISK_ANSWER, bean.getAnswer());
//        editor.putString(SpConstants.RISK_FUNDTYPE, bean.getFundType());
//        editor.putString(SpConstants.RISK_QUESTION, bean.getQuestion());
//        editor.commit();
//    }

//    public static RiskBean getRiskFromGlobal(Context context){
//        SharedPreferences setPreferences = context.getSharedPreferences(
//                GLOBALNAME, Context.MODE_PRIVATE);
//        RiskBean bean = new RiskBean();
//        bean.setRiskLevel(setPreferences.getInt(SpConstants.RISK_LEVEL, 0));
//        bean.setAnswer(setPreferences.getString(SpConstants.RISK_ANSWER, ""));
//        bean.setFundType(setPreferences.getString(SpConstants.RISK_FUNDTYPE, ""));
//        bean.setQuestion(setPreferences.getString(SpConstants.RISK_QUESTION, ""));
//        return bean;
//    }

    public static Boolean getBooleanFromGlobal(Context context, String strKey) {
        SharedPreferences setPreferences = context.getSharedPreferences(
                GLOBALNAME, Context.MODE_PRIVATE);
        return setPreferences.getBoolean(strKey, false);
    }

    public static Boolean getBooleanFromGlobal(Context context, String strKey,
                                               Boolean strDefault) {
        SharedPreferences setPreferences = context.getSharedPreferences(
                GLOBALNAME, Context.MODE_PRIVATE);
        return setPreferences.getBoolean(strKey, strDefault);
    }

    public static Boolean deleteFromGlobal(Context context, String strKey) {
        SharedPreferences setPreferences = context.getSharedPreferences(
                GLOBALNAME, Context.MODE_PRIVATE);
        return setPreferences.edit().remove(strKey).commit();
    }


    public static void putBooleanToGlobal(Context context, String strKey,
                                          Boolean strData) {
        SharedPreferences activityPreferences = context.getSharedPreferences(
                GLOBALNAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = activityPreferences.edit();
        editor.putBoolean(strKey, strData);
        editor.apply();
    }

    public static int getIntFromGlobal(Context context, String strKey) {
        SharedPreferences setPreferences = context.getSharedPreferences(
                GLOBALNAME, Context.MODE_PRIVATE);
        return setPreferences.getInt(strKey, -1);
    }

    public static int getIntFromGlobal(Context context, String strKey, int strDefault) {
        SharedPreferences setPreferences = context.getSharedPreferences(
                GLOBALNAME, Context.MODE_PRIVATE);
        return setPreferences.getInt(strKey, strDefault);
    }

    public static void putIntToGlobal(Context context, String strKey, int strData) {
        SharedPreferences activityPreferences = context.getSharedPreferences(
                GLOBALNAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = activityPreferences.edit();
        editor.putInt(strKey, strData);
        editor.apply();
    }

    public static long getLongFromGrobal(Context context, String strKey) {
        SharedPreferences setPreferences = context.getSharedPreferences(
                GLOBALNAME, Context.MODE_PRIVATE);
        return setPreferences.getLong(strKey, -1);
    }

    public static long getLongFromGlobal(Context context, String strKey, long strDefault) {
        SharedPreferences setPreferences = context.getSharedPreferences(
                GLOBALNAME, Context.MODE_PRIVATE);
        return setPreferences.getLong(strKey, strDefault);
    }

    public static void putLongToGlobal(Context context, String strKey, long strData) {
        SharedPreferences activityPreferences = context.getSharedPreferences(
                GLOBALNAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = activityPreferences.edit();
        editor.putLong(strKey, strData);
        editor.apply();
    }

    public static void clear(Context context,String spName){
        SharedPreferences edit = context.getSharedPreferences(spName,Context.MODE_PRIVATE);
        edit.edit().clear().apply();
    }

    public static void putStringToAccount(Context context,String strKey,String strData){
        SharedPreferences activityPreferences = context.getSharedPreferences(
                ACCOUNTINFO, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = activityPreferences.edit();
        editor.putString(strKey, strData);
        editor.apply();
    }

    public static String getStringFromAccount(Context context, String strKey, String defaultString){
        SharedPreferences activityPreferences = context.getSharedPreferences(
                ACCOUNTINFO, Context.MODE_PRIVATE);
        return activityPreferences.getString(strKey,defaultString);
    }

    public static String getStringFromAccount(Context context, String strKey){
        SharedPreferences activityPreferences = context.getSharedPreferences(
                ACCOUNTINFO, Context.MODE_PRIVATE);
        return activityPreferences.getString(strKey,"");
    }

    public static void putIntToAccount(Context context, String strKey, int data){
        SharedPreferences activityPreferences = context.getSharedPreferences(
                ACCOUNTINFO, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = activityPreferences.edit();
        editor.putInt(strKey, data);
        editor.apply();
    }

    public static int getIntFromAccount(Context context,String strKey){
        SharedPreferences sf = context.getSharedPreferences(
                ACCOUNTINFO, Context.MODE_PRIVATE);
        return sf.getInt(strKey,0);
    }

    public static void putBooleantoAccount(Context context,String strKey,boolean data){
        SharedPreferences sp = context.getSharedPreferences(
                ACCOUNTINFO,Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = sp.edit();
        edit.putBoolean(strKey,data);
        edit.apply();
    }

    public static boolean getBooleanFromAccount(Context context,String strKey){
        SharedPreferences sp = context.getSharedPreferences(
                ACCOUNTINFO,Context.MODE_PRIVATE);
        return sp.getBoolean(strKey,false);
    }

    public static void cleanGlobal(Context context){
        SharedPreferences sp = context.getSharedPreferences(
                GLOBALNAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = sp.edit();
//        edit.remove(SpConstants.RISK_LEVEL)
//                .remove(SpConstants.RISK_ANSWER)
//                .remove(SpConstants.RISK_FUNDTYPE)
//                .remove(SpConstants.RISK_QUESTION)
//                .remove(Constants.LAST_SUCCESS_BIZ_NUM)
//                .commit();
        edit.clear().apply();
    }
}
